﻿using KWT.DRG_DIP.API.Infrastructure;
using KWT.DRG_DIP.DB;
using KWT.DRG_DIP.ISvc;
using KWT.DRG_DIP.PO.Basic;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using System.Runtime.CompilerServices;

namespace KWT.DRG_DIP.API.Controllers.Basic
{
    /// <summary>
    /// 院区管理
    /// </summary>
    [ApiExplorerSettings(GroupName = "basic")]
    public class AreaController : BasicControllerBase
    {
        public EFContext ef { get; set; }
        /// <summary>
        /// 列表
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public OutputResult List()
        {
            string tanentID = HttpContext.Request.Headers["TenantID"];
            var list = ef.Set<Tenant_Area>().Where(x => x.TenantID == tanentID).ToList();
            return Output(list.Count > 0, "", "", list);
        }

        /// <summary>
        /// 列表（级联组件使用）
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public OutputResult CascaderList()
        {
            string tanentID = HttpContext.Request.Headers["TenantID"];
            var list = ef.Set<Tenant_Area>().Where(x => x.TenantID == tanentID).Select(x => new { label = x.Name, value = x.AreaID, children = new List<string>(), loading = false, type = "area" }).ToList();
            return Output(list.Count > 0, "", "", list);
        }

        /// <summary>
        /// 列表(当前用户)
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public OutputResult ListByUser()
        {
            var user = CurrentUser;
            if (user.IsAuth)
            {
                var list = ef.Set<Tenant_Area>().Where(x => user.AreaIDs.Contains(x.AreaID)).Select(x => new { x.AreaID, x.Name }).AsNoTracking().ToList();
                return Output(list.Count > 0, "", "", list);
            }
            else
            {
                var list = ef.Set<Tenant_Area>().Select(x => new { x.AreaID, x.Name }).AsNoTracking().ToList();
                return Output(list.Count > 0, "", "", list);
            }
        }
    }
}
